package com.chart.web.controller.chart.sta;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.chart.chart.domian.ChartConfigs;
import com.chart.chart.domian.ChartLegendData;
import com.chart.chart.domian.dto.ChartDateTto;
import com.chart.chart.domian.utils.ChartValidator;
import com.chart.chart.domian.vo.ChartSmartVo;
import com.chart.chart.domian.vo.ChartXData;
import com.chart.chart.domian.vo.ChartXDataVo;
import com.chart.chart.service.IChartConfigsService;
import com.chart.common.core.controller.BaseController;
import com.chart.common.core.domain.AjaxResult;
import com.chart.common.utils.ChartDataUtils;
import com.chart.common.utils.ColorUtils;
import com.chart.common.utils.ExcelDataUtils;
import com.chart.common.utils.ExcelUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 折线图
 *
 * @author chart
 */
@RestController
@RequestMapping("/line/chart")
public class LineChartController extends BaseController {


    @Autowired
    private IChartConfigsService chartConfigsService;

    //多线条圆滑折线图
    @PostMapping("/multiLineSmoothLine/importData")
    public AjaxResult multiLineSmoothLine(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);
    }


    /*   //多线条圆滑折线图
       @PostMapping("/multiLineSmoothLine/importData")
       public AjaxResult multiLineSmoothLine(MultipartFile file,Long configId) throws Exception {
           XSSFWorkbook workbook = null;
           try {
               workbook = new XSSFWorkbook(file.getInputStream());
               Sheet sheet = workbook.getSheetAt(1);
               //获取
               List<List<String>> getdata = ExcelDataUtils.getData(sheet);

               if (CollectionUtils.isEmpty(getdata)) {
                   return AjaxResult.error("请填充数据");
               }


               List<List<Object>> dataList = new ArrayList<>();
               for (int i = 0; i < getdata.size(); i++) {
                   List<String> rowData = getdata.get(i);
                   ArrayList<String> newList = new ArrayList<>();
                   for (int i1 = 0; i1 < rowData.size(); i1++) {
                       String s = rowData.get(i1);
                       if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                           newList.add(s);
                       }
                   }
                   if (!CollectionUtils.isEmpty(newList)) {
                   if (i == 0) {
                       List<Object> lists = new ArrayList<>();
                       lists.addAll(newList);
                       dataList.add(lists);
                   } else {
                           List<Object> singleData = new ArrayList<>();
                           for (int i1 = 0; i1 < newList.size(); i1++) {
                               if (i1==0) {
                                   String s = newList.get(i1);
                                   if (!ObjectUtils.isEmpty(s)) {
                                       singleData.add(newList.get(i1));
                                   }else {
                                       singleData.add(0);
                                   }
                               }else {
                                       singleData.add(Double.parseDouble(newList.get(i1)));
                                   }
                               }
                           dataList.add(singleData);
                           }
                       }
                   }


               ChartXDataVo xData = new ChartXDataVo();
               xData.setSource(dataList);


               ChartConfigs configs = chartConfigsService.getById(configId);
               JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
               jsonObject.put("dataset", xData);

               List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
               JSONObject o = series.get(1);
               List<JSONObject> seriesList = new ArrayList<>();
               for (int i = 1; i < getdata.get(0).size(); i++) {
                   JSONObject o1 = new JSONObject();
                   o1.putAll(o);
                   o1.put("name", getdata.get(0).get(i));
                   seriesList.add(o1);
               }
               jsonObject.put("series", seriesList);

               JSONObject legend = (JSONObject) jsonObject.get("legend");
               List<String> fistName = getdata.get(0);
               fistName.remove(0);
               List<ChartLegendData> chartLegendData = new ArrayList<>();
               for (int i = 0; i < fistName.size(); i++) {
                   ChartLegendData data = new ChartLegendData();
                   data.setName(fistName.get(i));
                   chartLegendData.add(data);
               }
               legend.put("data", chartLegendData);
               jsonObject.put("legend", legend);


               return AjaxResult.success(JSON.toJSONString(jsonObject));
           }finally {
               workbook.close();
           }
       }*/
    private List<List<String>> wrapListMapToList(List<Map<String, Object>> dataList) {
        List<List<String>> data = new ArrayList<>();//  [["1","4"],[33,666]]
        for (Map<String, Object> row : dataList) {
            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                rowData.add(entry.getValue().toString());
            }
            data.add(rowData);
        }
        return data;
    }

    private List<List<String>> wrapListMapToListy(List<Map<String, Object>> dataList) {
        List<List<String>> data = new ArrayList<>();

        // Assuming the first map entry defines the columns
        Map<String, Object> firstRow = dataList.get(0);
        List<String> columns = new ArrayList<>(firstRow.values().stream().map(Object::toString).collect(Collectors.toList()));
        data.add(columns);

        for (int i = 1; i < dataList.size(); i++) {
            Map<String, Object> row = dataList.get(i);
            List<String> rowData = new ArrayList<>();
            rowData.addAll(row.values().stream().map(Object::toString).collect(Collectors.toList()));
            data.add(rowData);
        }

        return data;
    }


    //多线条圆滑折线图
    @PostMapping("/multiLineSmoothLine/v2/importData")
    public AjaxResult vmultiLineSmoothLine(@RequestBody List<Map<String, Object>> dataLists, Long configId) throws Exception {
        //XSSFWorkbook workbook = null;
//        try {
        //   workbook = new XSSFWorkbook(file.getInputStream());

        Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataLists);


        //获取
        List<List<String>> getdata = ExcelDataUtils.getData(sheet);

        if (CollectionUtils.isEmpty(getdata)) {
            return AjaxResult.error("请填充数据");
        }

        List<List<Object>> dataList = new ArrayList<>();
        for (int i = 0; i < getdata.size(); i++) {
            List<String> rowData = getdata.get(i);
            ArrayList<String> newList = new ArrayList<>();
            for (int i1 = 0; i1 < rowData.size(); i1++) {
                String s = rowData.get(i1);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    newList.add(s);
                }
            }
            if (!CollectionUtils.isEmpty(newList)) {
                if (i == 0) {
                    List<Object> lists = new ArrayList<>();
                    lists.addAll(newList);
                    dataList.add(lists);
                } else {
                    List<Object> singleData = new ArrayList<>();
                    for (int i1 = 0; i1 < newList.size(); i1++) {
                        if (i1 == 0) {
                            String s = newList.get(i1);
                            if (!ObjectUtils.isEmpty(s)) {
                                singleData.add(newList.get(i1));
                            } else {
                                singleData.add(0);
                            }
                        } else {
                            singleData.add(Double.parseDouble(newList.get(i1)));
                        }
                    }
                    dataList.add(singleData);
                }
            }
        }


        ChartXDataVo xData = new ChartXDataVo();
        xData.setSource(dataList);


        ChartConfigs configs = chartConfigsService.getById(configId);
        JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
        jsonObject.put("dataset", xData);

        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        JSONObject o = series.get(1);
        List<JSONObject> seriesList = new ArrayList<>();
        for (int i = 1; i < getdata.get(0).size(); i++) {
            JSONObject o1 = new JSONObject();
            o1.putAll(o);
            o1.put("name", getdata.get(0).get(i));
            seriesList.add(o1);
        }
        jsonObject.put("series", seriesList);

        JSONObject legend = (JSONObject) jsonObject.get("legend");
        List<String> fistName = getdata.get(0);
        fistName.remove(0);
        List<ChartLegendData> chartLegendData = new ArrayList<>();
        for (int i = 0; i < fistName.size(); i++) {
            ChartLegendData data = new ChartLegendData();
            data.setName(fistName.get(i));
            chartLegendData.add(data);
        }
        legend.put("data", chartLegendData);
        jsonObject.put("legend", legend);


        return AjaxResult.success(JSON.toJSONString(jsonObject));


    }


    //心电图
    @PostMapping("/electrocardiogramCurve/importData")
    public AjaxResult electrocardiogramCurve(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);

    }



/*
    //心电图
    @PostMapping("/electrocardiogramCurve/importData")
    public AjaxResult electrocardiogramCurve(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata =new ArrayList<>();
            List<String> xlist = getdata.get(0);
            for (int i = 1; i < xlist.size(); i++) {
                String s = xlist.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    xdata.add(s);
                }
            }
//        xdata.remove(0);

            List<ChartXDataVo> chartXDataVos = new ArrayList<>();
            List<String> ydata = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<List<Object>>  groupList= new ArrayList<>();
                ChartXDataVo sData = new ChartXDataVo();
                List<String> list = getdata.get(i);
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1==0) {
                            ydata.add(s);
                        }else {
                            List<Object> cellData = new ArrayList<>();
                            cellData.add(xdata.get(i1-1));
                            cellData.add(Double.parseDouble(s));
                            groupList.add(cellData);
                        }
                    }
                }
                sData.setSource(groupList);
                chartXDataVos.add(sData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(1);

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < ydata.size() ; i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("datasetIndex", i);
                jsonObject2.put("name", ydata.get(i));
                jsonObjects.add(jsonObject2);
            }
            jsonObject.put("series", jsonObjects);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
//            xdata.remove(0);
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 0; i < ydata.size(); i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(ydata.get(i));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }*/

    //心电图
    @PostMapping("/electrocardiogramCurve/v2/importData")
    public AjaxResult velectrocardiogramCurve(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
        // XSSFWorkbook workbook = null;
        try {
       /*     workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);
*/
            List<List<String>> getdata = wrapListMapToList(dataList);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata = new ArrayList<>();
            List<String> xlist = getdata.get(0);
            for (int i = 1; i < xlist.size(); i++) {
                String s = xlist.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    xdata.add(s);
                }
            }
//        xdata.remove(0);

            List<ChartXDataVo> chartXDataVos = new ArrayList<>();
            List<String> ydata = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<List<Object>> groupList = new ArrayList<>();
                ChartXDataVo sData = new ChartXDataVo();
                List<String> list = getdata.get(i);
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1 == 0) {
                            ydata.add(s);
                        } else {
                            List<Object> cellData = new ArrayList<>();
                            cellData.add(xdata.get(i1 - 1));
                            cellData.add(Double.parseDouble(s));
                            groupList.add(cellData);
                        }
                    }
                }
                sData.setSource(groupList);
                chartXDataVos.add(sData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(1);

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < ydata.size(); i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("datasetIndex", i);
                jsonObject2.put("name", ydata.get(i));
                jsonObjects.add(jsonObject2);
            }
            jsonObject.put("series", jsonObjects);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
//            xdata.remove(0);
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 0; i < ydata.size(); i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(ydata.get(i));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            //  workbook.close();
        }
    }

    //多层圆滑折线图
    @PostMapping("/multiLayerSmoothLine/importData")
    public AjaxResult multiLayerSmoothLine(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);
    }


    /*    //多层圆滑折线图
        @PostMapping("/multiLayerSmoothLine/importData")
        public AjaxResult multiLayerSmoothLine(MultipartFile file,Long configId) throws Exception {
            XSSFWorkbook workbook = null;
            try {
                workbook = new XSSFWorkbook(file.getInputStream());
                Sheet sheet = workbook.getSheetAt(1);
                //获取
                List<List<String>> getdata = ExcelDataUtils.getData(sheet);

                if (CollectionUtils.isEmpty(getdata)) {
                    return AjaxResult.error("请填充数据");
                }
                List<String> fistName = new ArrayList<>();
                List<String> data = getdata.get(0);
                for (int i = 0; i < data.size(); i++) {
                    String s = data.get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        fistName.add(s);
                    }
                }

                List<List<String>> newList = new ArrayList<>();
                for (int i = 1; i < getdata.size(); i++) {
                    List<String> list = getdata.get(i);
                    if (!CollectionUtils.isEmpty(list)) {
                        List<String> list1 = new ArrayList<>();
                        for (int i1 = 0; i1 < list.size(); i1++) {
                            String s = list.get(i1);
                            if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                                list1.add(s);
                            }
                        }
                        newList.add(list1);
                    }
                }

                Map<String, List<List<String>>> map = new HashMap<>();
                newList.add(0,fistName);
                map.put("source",newList);
                ArrayList<Map> maps = new ArrayList<>();
                maps.add(map);

                ChartConfigs configs = chartConfigsService.getById(configId);
                JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

                jsonObject.put("dataset",maps);

                List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
                JSONObject json = series.get(0);

                List<JSONObject> jsonObjects = new ArrayList<>();
                ArrayList<Map> namesMap = new ArrayList<>();
                for (int i = 1; i < fistName.size(); i++) {
                    String s = fistName.get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        JSONObject jsonObject1 = new JSONObject();
                        jsonObject1.putAll(json);

                        jsonObject1.put("name",s);
                        HashMap<String, String> nameMap = new HashMap<>();
                        nameMap.put("name",s);
                        namesMap.add(nameMap);
                        jsonObjects.add(jsonObject1);
                    }
                }
                jsonObject.put("series",jsonObjects);

                JSONObject legend = (JSONObject) jsonObject.get("legend");
                legend.put("data",namesMap);
                jsonObject.put("legend",legend);

                return AjaxResult.success(JSON.toJSONString(jsonObject));
            }finally {
                workbook.close();
            }
        }*/
    //多层圆滑折线图
    @PostMapping("/multiLayerSmoothLine/v2/importData")
    public AjaxResult vmultiLayerSmoothLine(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);
    }


    /*    //河流图
        @PostMapping("/riverMap/importData")
        public AjaxResult riverMap(MultipartFile file,Long configId) throws Exception {
            XSSFWorkbook workbook = null;
            try {
                workbook = new XSSFWorkbook(file.getInputStream());
                Sheet sheet = workbook.getSheetAt(1);
                //获取
                List<List<String>> getdata = ExcelDataUtils.getData(sheet);

                if (CollectionUtils.isEmpty(getdata)) {
                    return AjaxResult.error("请填充数据");
                }

                List<String> fistName = getdata.get(0);

                ChartXData xDatas = new ChartXData();

                List<String> strings = getdata.get(1);
                String s = strings.get(3);

                List<List<String>> list = new ArrayList<>();
                for (int i = 0; i < getdata.size(); i++) {
                    List<String> list1 = getdata.get(i);
                    if (!CollectionUtils.isEmpty(list1)) {
                        List<String> list2 = new ArrayList<>();
                        for (int i1 = 0; i1 < list1.size(); i1++) {
                            String s1 = list1.get(i1);
                            if (!ObjectUtils.isEmpty(s1) && !s1.equals("")) {
                                list2.add(s1);
                            }
                        }
                        if (i == 0) {
                            list.add(list2);
                        } else {
                            list.add(list2);
                        }
                    }
                }

                xDatas.setSource(list);


                ChartConfigs configs = chartConfigsService.getById(configId);
                JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
                List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
                List<JSONObject> sDatas = new ArrayList<>();


    *//*            for (Integer integer = 1; integer < fistName.size(); integer++) {
                JSONObject jsonObject1 = series.get(integer - 1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                JSONObject endLabel = (JSONObject) jsonObject2.get("endLabel");
                endLabel.put("formatter",fistName.get(integer));
                jsonObject2.put("endLabel",endLabel);

                JSONObject areaStyle = (JSONObject) jsonObject2.get("areaStyle");
                areaStyle.put("origin",s);
                sDatas.add(jsonObject2);
            }*//*
            for (Integer integer = 1; integer < fistName.size(); integer++) {
                JSONObject jsonObject1 = series.get(integer - 1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);

                // 确保endLabel不为null
                JSONObject endLabel = (JSONObject) jsonObject2.get("endLabel");
                if (endLabel == null) {
                    endLabel = new JSONObject();
                }
                endLabel.put("formatter", fistName.get(integer));
                jsonObject2.put("endLabel", endLabel);

                // 确保areaStyle不为null
                JSONObject areaStyle = (JSONObject) jsonObject2.get("areaStyle");
                if (areaStyle == null) {
                    areaStyle = new JSONObject();
                }
                areaStyle.put("origin", s);
                jsonObject2.put("areaStyle", areaStyle);

                sDatas.add(jsonObject2);
            }


            jsonObject.put("dataset", xDatas);
            jsonObject.put("series", sDatas);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }*/

    public static List<List<String>> extractDataFromList(List<Map<String, Object>> dataList) {
        List<List<String>> extractedData = new ArrayList<>();
        for (Map<String, Object> row : dataList) {
            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                rowData.add(entry.getValue().toString());
            }
            extractedData.add(rowData);
        }
        return extractedData;
    }

    @PostMapping("/riverMap/importData")
    public AjaxResult riverMap(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点并替换source节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        for (JsonNode node : dataset) {
            if (node.has("source")) {
                ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                break;
            }
        }

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }


        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);


        // 处理颜色字段
        ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(chartValue);
        // 获取 color 字段
        JsonNode colorNode = jsonNode.path("color");

        List<String> baseColors = new ArrayList<>();
        // 遍历 color 数组并将每个颜色值添加到 baseColors 集合中
        if (colorNode.isArray()) {
            for (JsonNode color : colorNode) {
                String colorValue = color.asText();
                baseColors.add(colorValue);
            }
        }

        int beforeSize = baseColors.size();
        int afterSize = lists.get(0).size();
        if (beforeSize < afterSize) {
            chartValueJson = ColorUtils.processChartColorsHert(JSON.toJSONString(chartValueJson), afterSize);
        }
        String updatedChartValue = chartValueJson.toString();
        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);

    }
/*    @PostMapping("/riverMap/importData")
    public AjaxResult riverMap(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);
    }*/

    //河流图
    @PostMapping("/riverMap/v2/importData")
    public AjaxResult vriverMap(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
        //XSSFWorkbook workbook = null;
        try {
     /*       workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取

*/

            Sheet sheet = ExcelUtils.convertDataListToSheetRemoveNineAndOne(dataList);

            List<List<String>> getdata = ExcelDataUtils.getData(sheet);
            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> fistName = getdata.get(0);

            ChartXData xDatas = new ChartXData();

            List<String> strings = getdata.get(1);
            String s = strings.get(3);

            List<List<String>> list = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> list1 = getdata.get(i);
                if (!CollectionUtils.isEmpty(list1)) {
                    List<String> list2 = new ArrayList<>();
                    for (int i1 = 0; i1 < list1.size(); i1++) {
                        String s1 = list1.get(i1);
                        if (!ObjectUtils.isEmpty(s1) && !s1.equals("")) {
                            list2.add(s1);
                        }
                    }
                    if (i == 0) {
                        list.add(list2);
                    } else {
                        list.add(list2);
                    }
                }
            }

            xDatas.setSource(list);


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            List<JSONObject> sDatas = new ArrayList<>();


            for (Integer integer = 1; integer < fistName.size(); integer++) {
                JSONObject jsonObject1 = series.get(integer - 1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                JSONObject endLabel = (JSONObject) jsonObject2.get("endLabel");
                endLabel.put("formatter", fistName.get(integer));
                jsonObject2.put("endLabel", endLabel);

                JSONObject areaStyle = (JSONObject) jsonObject2.get("areaStyle");
                areaStyle.put("origin", s);
                sDatas.add(jsonObject2);
            }


            jsonObject.put("dataset", xDatas);
            jsonObject.put("series", sDatas);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
//            workbook.close();
        }
    }

    //面板ECDF图
    @PostMapping("/panelECDF/importData")
    public AjaxResult importData(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);
    }



/*    //面板ECDF图
    @PostMapping("/panelECDF/importData")
    public AjaxResult importData(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }


            List<String> xdata = getdata.get(0);

            ArrayList<ChartXDataVo> chartXDataVos = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<List<Object>> groupList = new ArrayList<>();
                ChartXDataVo sData = new ChartXDataVo();
                List<String> colData = getdata.get(i);
                for (int i1 = 1; i1 < colData.size(); i1++) {
                    List<Object> cellData = new ArrayList<>();
                    cellData.add(xdata.get(i1));
                    cellData.add(Double.parseDouble(colData.get(i1)));
                    groupList.add(cellData);
                }
                sData.setSource(groupList);
                chartXDataVos.add(sData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            int size = getdata.size();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject o = series.get(1);
            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 1; i < size; i++) {
                JSONObject o1 = new JSONObject();
                o1.putAll(o);
                o1.put("name", getdata.get(i).get(0));
                o1.put("datasetIndex", i - 1);
                seriesList.add(o1);
            }
            jsonObject.put("series", seriesList);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 1; i < size; i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(getdata.get(i).get(0));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }*/


    //面板ECDF图
    @PostMapping("/panelECDF/v2/importData")
    public AjaxResult vimportData(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
        /*    workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);
*/

            List<List<String>> getdata = wrapListMapToList(dataList);
            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }


            List<String> xdata = getdata.get(0);

            ArrayList<ChartXDataVo> chartXDataVos = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<List<Object>> groupList = new ArrayList<>();
                ChartXDataVo sData = new ChartXDataVo();
                List<String> colData = getdata.get(i);
                for (int i1 = 1; i1 < colData.size(); i1++) {
                    List<Object> cellData = new ArrayList<>();
                    cellData.add(xdata.get(i1));
                    cellData.add(Double.parseDouble(colData.get(i1)));
                    groupList.add(cellData);
                }
                sData.setSource(groupList);
                chartXDataVos.add(sData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            int size = getdata.size();
            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject o = series.get(1);
            List<JSONObject> seriesList = new ArrayList<>();
            for (int i = 1; i < size; i++) {
                JSONObject o1 = new JSONObject();
                o1.putAll(o);
                o1.put("name", getdata.get(i).get(0));
                o1.put("datasetIndex", i - 1);
                seriesList.add(o1);
            }
            jsonObject.put("series", seriesList);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 1; i < size; i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(getdata.get(i).get(0));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }

    //单一折线图
    @PostMapping("/basic/importData")
    public AjaxResult basic(@RequestBody ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = ChartDataUtils.extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        // 更新dataset中的source节点
        dataset = ChartDataUtils.updateDatasetSources(dataset, lists, objectMapper);

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }

        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 在这里添加新的逻辑来处理 datasetIndex 字段
        for (int i = 0; i < seriesArray.size(); i++) {
            ObjectNode seriesObject = (ObjectNode) seriesArray.get(i);
            if (seriesObject.has("datasetIndex")) {
                seriesObject.put("datasetIndex", i);
            } else {
                seriesObject.put("datasetIndex", i);
            }
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        String updatedChartValue = chartValueJson.toString();

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);
    }


/*    //单一折线图
    @PostMapping("/basic/importData")
    public AjaxResult basic(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<Map> roomName = new ArrayList<>();


            List<String> strings = getdata.get(1);
            HashMap<String, String> map = new HashMap<>();
            map.put("name", strings.get(0));
            roomName.add(map);

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",roomName);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            List<JSONObject> list = new ArrayList<>();
            for (int i = 0; i < series.size(); i++) {
                JSONObject o1 = series.get(i);
                o1.put("name",roomName.get(i).get("name"));
                List<String> list1 =getdata.get(i+1);
                ArrayList<String> newList = new ArrayList<>();
                for (int i1 = 1; i1 < list1.size(); i1++) {
                    String s = list1.get(i1);
                    if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        newList.add(s);
                    }
                }
                o1.put("data",newList);
                list.add(o1);
            }

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            List<String> strings1 = getdata.get(0);
            strings1.remove(0);

            xAxis.put("data",strings1);

            jsonObject.put("xAxis",xAxis);
            jsonObject.put("legend",legend);
            jsonObject.put("series",list);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }*/

    //单一折线图
    @PostMapping("/basic/v2/importData")
    public AjaxResult vbasic(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
        /*    workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);
*/
            List<List<String>> getdata = wrapListMapToList(dataList);
            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<Map> roomName = new ArrayList<>();


            List<String> strings = getdata.get(1);
            HashMap<String, String> map = new HashMap<>();
            map.put("name", strings.get(0));
            roomName.add(map);

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data", roomName);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");

            List<JSONObject> list = new ArrayList<>();
            for (int i = 0; i < series.size(); i++) {
                JSONObject o1 = series.get(i);
                o1.put("name", roomName.get(i).get("name"));
                List<String> list1 = getdata.get(i + 1);
                ArrayList<String> newList = new ArrayList<>();
                for (int i1 = 1; i1 < list1.size(); i1++) {
                    String s = list1.get(i1);
                    if (org.apache.commons.lang3.ObjectUtils.isNotEmpty(s) && !s.equals("")) {
                        newList.add(s);
                    }
                }
                o1.put("data", newList);
                list.add(o1);
            }

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            List<String> strings1 = getdata.get(0);
            strings1.remove(0);

            xAxis.put("data", strings1);

            jsonObject.put("xAxis", xAxis);
            jsonObject.put("legend", legend);
            jsonObject.put("series", list);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }

    //阶梯折线图
    @PostMapping("/stepLine/importData")
    public AjaxResult stepLine(@RequestBody ChartDateTto chartDateTto) throws Exception {

        return null;
    }


/*    //阶梯折线图
    @PostMapping("/stepLine/importData")
    public AjaxResult stepLine(MultipartFile file,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
            workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xlist = new ArrayList<>();
            List<String> xdata = getdata.get(0);
            for (int i = 1; i < xdata.size(); i++) {
                String s = xdata.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    xlist.add(s);
                }
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());



            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject json = series.get(0);

            List<Map> maps = new ArrayList<>();
            List<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    ArrayList<String> newList = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            newList.add(s);
                        }
                    }
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.putAll(json);
                    jsonObject1.put("name",newList.get(0));
                    Map<String, String> map = new HashMap<>();
                    map.put("name",newList.get(0));
                    maps.add(map);
                    newList.remove(0);
                    jsonObject1.put("data",newList);
                    jsonObjects.add(jsonObject1);
                }
            }

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            xAxis.put("data",xlist);
            jsonObject.put("xAxis",xAxis);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",maps);
            jsonObject.put("legend",legend);

            jsonObject.put("series",jsonObjects);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
            workbook.close();
        }
    }*/


    //阶梯折线图
    @PostMapping("/stepLine/v2/importData")
    public AjaxResult vstepLine(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {
         /*   workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);
*/
            List<List<String>> getdata = wrapListMapToList(dataList);
            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xlist = new ArrayList<>();
            List<String> xdata = getdata.get(0);
            for (int i = 1; i < xdata.size(); i++) {
                String s = xdata.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    xlist.add(s);
                }
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());


            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject json = series.get(0);

            List<Map> maps = new ArrayList<>();
            List<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    ArrayList<String> newList = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            newList.add(s);
                        }
                    }
                    JSONObject jsonObject1 = new JSONObject();
                    jsonObject1.putAll(json);
                    jsonObject1.put("name", newList.get(0));
                    Map<String, String> map = new HashMap<>();
                    map.put("name", newList.get(0));
                    maps.add(map);
                    newList.remove(0);
                    jsonObject1.put("data", newList);
                    jsonObjects.add(jsonObject1);
                }
            }

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            xAxis.put("data", xlist);
            jsonObject.put("xAxis", xAxis);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data", maps);
            jsonObject.put("legend", legend);

            jsonObject.put("series", jsonObjects);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
            workbook.close();
        }
    }

    @PostMapping("/doublex/importData")
    public AjaxResult doublex(@RequestBody ChartDateTto chartDateTto) throws Exception {
        Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
        List<List<String>> getdata = ExcelDataUtils.getData(sheet);
        if (CollectionUtils.isEmpty(getdata)) {
            return AjaxResult.error("请填充数据");
        }

        List<String> groupName = new ArrayList<>();
        List<String> x1 = new ArrayList<>();
        List<String> x2 = new ArrayList<>();
        List<String> y1 = new ArrayList<>();
        List<String> y2 = new ArrayList<>();

        for (int i = 1; i < getdata.size(); i++) {
            List<String> list = getdata.get(i);
            if (!CollectionUtils.isEmpty(list)) {
                List<String> data = new ArrayList<>();
                for (String s : list) {
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        data.add(s);
                    }
                }
                if (data.size() == 1) {
                    groupName.add(data.get(0));
                } else {
                    if (groupName.size() == 1) {
                        for (int i1 = 0; i1 < data.size(); i1++) {
                            String s = data.get(i1);
                            if (i1 == 0) {
                                x1.add(s);
                            } else {
                                y1.add(s);
                            }
                        }
                    } else {
                        for (int i1 = 0; i1 < data.size(); i1++) {
                            String s = data.get(i1);
                            if (i1 == 0) {
                                x2.add(s);
                            } else {
                                y2.add(s);
                            }
                        }
                    }
                }
            }
        }
   /*     JSONObject jsonObject = JSON.parseObject(chartDateTto.getChartValue());
        Object legendObject = jsonObject.get("legend");
        JSONObject legend;
        if (legendObject instanceof JSONObject) {
            legend = (JSONObject) legendObject;
        } else if (legendObject instanceof JSONArray) {
            JSONArray legendArray = (JSONArray) legendObject;
            legend = new JSONObject();
            legend.put("data", legendArray);
        } else {
            throw new IllegalStateException("Expected JSONObject or JSONArray for 'legend' but found " + legendObject.getClass());
        }
        List<Map<String, Object>> maps = new ArrayList<>();
        for (String name : groupName) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", name);
            maps.add(map);
        }
        legend.put("data", maps);
        jsonObject.put("legend", legend);*/


        JSONObject jsonObject = JSON.parseObject(chartDateTto.getChartValue());
        Object legendObject = jsonObject.get("legend");

        if (legendObject instanceof JSONArray) {
            JSONArray legendArray = (JSONArray) legendObject;

            // 遍历 legendArray 并找到 "data" 数组
            for (int i = 0; i < legendArray.size(); i++) {
                JSONObject legend = legendArray.getJSONObject(i);

                // 仅操作 legend 中的 "data"
                Object dataObject = legend.get("data");
                if (dataObject instanceof JSONArray) {
                    JSONArray dataArray = (JSONArray) dataObject;
                    List<Map<String, Object>> maps = new ArrayList<>();
                    for (String name : groupName) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("name", name);
                        maps.add(map);
                    }
                    dataArray.addAll(maps);  // 保留原有数据并追加新数据
                    legend.put("data", dataArray);  // 更新 legend 的 "data"
                } else {
                    throw new IllegalStateException("Expected JSONArray for 'data' but found " + (dataObject == null ? "null" : dataObject.getClass()));
                }
            }
        } else {
            throw new IllegalStateException("Expected JSONArray for 'legend' but found " + (legendObject == null ? "null" : legendObject.getClass()));
        }

        jsonObject.put("legend", legendObject);


        // Handle 'xAxis' array
        List<JSONObject> xAxis = (List<JSONObject>) jsonObject.get("xAxis");
        for (int i = 0; i < xAxis.size(); i++) {
            JSONObject jsonObject1 = xAxis.get(i);
            jsonObject1.put("data", (i == 0) ? x1 : x2);
        }
        jsonObject.put("xAxis", xAxis);

        List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
        for (int i = 0; i < series.size(); i++) {
            JSONObject jsonObject1 = series.get(i);
            jsonObject1.put("name", groupName.get(i));
            jsonObject1.put("data", (i == 0) ? y1 : y2);
        }
        jsonObject.put("series", series);

        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
        newList.add(chartSmartVo);
        System.out.println(JSON.toJSONString(jsonObject));
        return AjaxResult.success(newList);
    }


    @PostMapping("/doublex/v2/importData")
    public AjaxResult v2doublex(@RequestBody List<Map<String, Object>> dataList, Long configId) throws Exception {
        // XSSFWorkbook workbook = null;
        try {
       /*     workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getData(sheet);
*/

            List<List<String>> getdata = wrapListMapToList(dataList);
            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> groupName = new ArrayList<>();
            List<String> x1 = new ArrayList<>();
            List<String> x2 = new ArrayList<>();

            List<String> y1 = new ArrayList<>();
            List<String> y2 = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                if (!CollectionUtils.isEmpty(list)) {
                    List<String> data = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            data.add(s);
                        }
                    }

                    if (data.size() == 1) {
                        groupName.add(data.get(0));
                    } else {
                        if (groupName.size() == 1) {
                            for (int i1 = 0; i1 < data.size(); i1++) {
                                String s = data.get(i1);
                                if (i1 == 0) {
                                    x1.add(s);
                                } else {
                                    y1.add(s);
                                }
                            }
                        } else {
                            for (int i1 = 0; i1 < data.size(); i1++) {
                                String s = data.get(i1);
                                if (i1 == 0) {
                                    x2.add(s);
                                } else {
                                    y2.add(s);
                                }
                            }
                        }
                    }
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            List<Map> maps = new ArrayList<>();
            for (int i = 0; i < groupName.size(); i++) {
                Map map = new HashMap<>();
                map.put("name", groupName.get(i));
                maps.add(map);
            }

            legend.put("data", maps);
            jsonObject.put("legend", legend);

            List<JSONObject> xAxis = (List<JSONObject>) jsonObject.get("xAxis");
            for (int i = 0; i < xAxis.size(); i++) {
                JSONObject jsonObject1 = xAxis.get(i);
                if (i == 0) {
                    jsonObject1.put("data", x1);
                } else {
                    jsonObject1.put("data", x2);
                }
            }
            jsonObject.put("xAxis", xAxis);

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            for (int i = 0; i < series.size(); i++) {
                JSONObject jsonObject1 = series.get(i);
                jsonObject1.put("name", groupName.get(i));
                if (i == 0) {
                    jsonObject1.put("data", y1);
                } else {
                    jsonObject1.put("data", y2);
                }
            }

            jsonObject.put("series", series);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
//            workbook.close();
        }
    }

    private static void saveSheetToFile(Sheet sheet, String fileName) {
        // 获取工作簿对象
        Workbook workbook = sheet.getWorkbook();

        try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
            // 将工作簿写入文件输出流
            workbook.write(fileOut);
            System.out.println("Excel file has been generated successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static Sheet loadSheetFromFile(String filePath) {
        try (FileInputStream fileIn = new FileInputStream(new File(filePath))) {
            Workbook workbook = WorkbookFactory.create(fileIn);
            return workbook.getSheetAt(0);  // 假设只需要第一个 sheet
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("/doubley/importData")
    public AjaxResult doubley(@RequestBody ChartDateTto chartDateTto) throws Exception {
        // 参数校验
        String validationMessage = ChartValidator.check(chartDateTto);
        if (validationMessage != null) {
        // 返回校验失败的提示信息给前端
            return AjaxResult.error(validationMessage);
        }
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        if (chartDateTto.getConfigId() == 48) {
            Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
            // 保存 Sheet 到本地文件
            saveSheetToFile(sheet, "C:/Users/Administrator/Desktop/老代码/yyy.xlsx");
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);
            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata = new ArrayList<>();
            List<String> names = new ArrayList<>();
            List<List<String>> strings = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                List<String> datas = new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1 == 0) {
                            names.add(s);
                        } else {
                            datas.add(s);
                        }
                    }
                }
                if (i == 0) {
                    xdata.addAll(datas);
                } else {
                    strings.add(datas);
                }
            }

            JSONObject jsonObject = JSON.parseObject(chartDateTto.getChartValue());

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            List<JSONObject> yAxis = (List<JSONObject>) jsonObject.get("yAxis");
            for (int i = 0; i < series.size(); i++) {
                JSONObject jsonObject1 = series.get(i);
                jsonObject1.put("name", names.get(i + 1));
                List<String> list = strings.get(i);
                int max = ExcelDataUtils.Max(list);
                int min = ExcelDataUtils.Min(list);
                JSONObject jsonObject2 = yAxis.get(i);
                jsonObject2.put("max", max);
                jsonObject2.put("min", min);
                jsonObject2.put("name", names.get(i + 1));
                if (i == 0) {
                    List<Map<String, Object>> maps = new ArrayList<>();
                    for (String s : list) {
                        double value = Double.parseDouble(s);
                        Map<String, Object> map = new HashMap<>();
                        map.put("value", value);
                        if (value > 0) {
                            map.put("itemStyle", rightColor);
                        } else {
                            map.put("itemStyle", leftColor);
                        }
                        maps.add(map);
                    }
                    jsonObject1.put("data", maps);
                } else {
                    jsonObject1.put("data", list);
                }
            }

            List<Map<String, String>> maps1 = new ArrayList<>();
            for (int i = 1; i < names.size(); i++) {
                Map<String, String> map = new HashMap<>();
                map.put("name", names.get(i));
                maps1.add(map);
            }
/*
            Object legendObject = jsonObject.get("legend");
            if (legendObject instanceof JSONArray) {
                JSONArray legendArray = (JSONArray) legendObject;
                JSONObject legend = new JSONObject();
                legend.put("data", maps1);
                legendArray.add(legend);
                jsonObject.put("legend", legendArray);
            } else if (legendObject instanceof JSONObject) {
                JSONObject legend = (JSONObject) legendObject;
                legend.put("data", maps1);
                jsonObject.put("legend", legend);
            }*/

            Object legendObject = jsonObject.get("legend");

            if (legendObject instanceof JSONArray) {
                JSONArray legendArray = (JSONArray) legendObject;

                // 遍历 legendArray 并找到 "data" 数组
                for (int i = 0; i < legendArray.size(); i++) {
                    JSONObject legend = legendArray.getJSONObject(i);

                    // 仅操作 legend 中的 "data"
                    Object dataObject = legend.get("data");
                    if (dataObject instanceof JSONArray) {
                        JSONArray dataArray = (JSONArray) dataObject;
                        dataArray.addAll(maps1);  // 保留原有数据并追加新数据
                    } else {
                        legend.put("data", maps1);  // 如果 "data" 不是数组，直接覆盖
                    }
                }

                jsonObject.put("legend", legendArray);

            } else if (legendObject instanceof JSONObject) {
                JSONObject legend = (JSONObject) legendObject;

                // 仅操作 legend 中的 "data"
                Object dataObject = legend.get("data");
                if (dataObject instanceof JSONArray) {
                    JSONArray dataArray = (JSONArray) dataObject;
                    dataArray.addAll(maps1);  // 保留原有数据并追加新数据
                } else {
                    legend.put("data", maps1);  // 如果 "data" 不是数组，直接覆盖
                }

                jsonObject.put("legend", legend);
            }



      /*      JSONObject xAxis = jsonObject.getJSONObject("xAxis");
            if (xAxis == null) {
                xAxis = new JSONObject();
            }
            xAxis.put("data", xdata);
            jsonObject.put("xAxis", xAxis);*/
            Object xAxisObject = jsonObject.get("xAxis");
            if (xAxisObject instanceof JSONArray) {
                JSONArray xAxisArray = (JSONArray) xAxisObject;
                for (int i = 0; i < xAxisArray.size(); i++) {
                    JSONObject xAxisItem = xAxisArray.getJSONObject(i);
                    xAxisItem.put("data", xdata);  // 只对 "data" 部分进行处理
                }
                jsonObject.put("xAxis", xAxisArray);
            } else if (xAxisObject instanceof JSONObject) {
                JSONObject xAxis = (JSONObject) xAxisObject;
                xAxis.put("data", xdata);  // 只对 "data" 部分进行处理
                jsonObject.put("xAxis", xAxis);
            }


            jsonObject.put("series", series);

            jsonObject.put("yAxis", yAxis);

            ArrayList<ChartSmartVo> newLista = new ArrayList<>();
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            chartSmartVo.setConfigValue(JSON.toJSONString(jsonObject));
            newLista.add(chartSmartVo);
            System.out.println(JSON.toJSONString(jsonObject));
            return AjaxResult.success(newLista);


/*            Sheet sheet = ExcelDataUtils.convertDataListToSheetsa(chartDateTto.getDataList());
            // 保存 Sheet 到本地文件
            saveSheetToFile(sheet, "C:/Users/Administrator/Desktop/老代码/yy.xlsx");
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);
            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata = new ArrayList<>();
            List<String> names = new ArrayList<>();
            List<List<String>> strings = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                List<String> datas =new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1==0) {
                            names.add(s);
                        }else {
                            datas.add(s);
                        }
                    }
                }
                if (i==0) {
                    xdata.addAll(datas);
                }else {
                    strings.add(datas);
                }
            }

           // ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(chartDateTto.getChartValue());

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            List<JSONObject> yAxis = (List<JSONObject>) jsonObject.get("yAxis");
            for (int i = 0; i < series.size(); i++) {
                if (i==0) {
                    JSONObject jsonObject1 = series.get(i);
                    jsonObject1.put("name",names.get(i+1));
                    List<String> list = strings.get(i);
                    //求出最大值（绝对值）并返回把最大值最高位+1的数值
                    int max = ExcelDataUtils.Max(list);
                    int min = ExcelDataUtils.Min(list);
                    JSONObject jsonObject2 = yAxis.get(i);
                    jsonObject2.put("max",max);
                    jsonObject2.put("min",min);
                    jsonObject2.put("name",names.get(i+1));
                    List<Map> maps = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        double s = Double.parseDouble(list.get(i1));
                        Map map = new HashMap<>();
                        map.put("value",s);
                        if (s>0) {
                            map.put("itemStyle",rightColor);
                        }else {
                            map.put("itemStyle",leftColor);
                        }
                        maps.add(map);
                    }
                    jsonObject1.put("data",maps);
                }else {
                    JSONObject jsonObject1 = series.get(i);
                    jsonObject1.put("name",names.get(i+1));
                    List<String> list = strings.get(i);
                    int max = ExcelDataUtils.Max(list);
                    int min = ExcelDataUtils.Min(list);
                    JSONObject jsonObject2 = yAxis.get(i);
                    jsonObject2.put("max",max);
                    jsonObject2.put("min",min);
                    jsonObject2.put("name",names.get(i+1));
                    jsonObject1.put("data",list);
                }
            }

            List<Map> maps1 = new ArrayList<>();
            for (int i = 1; i < names.size(); i++) {
                Map map = new HashMap<>();
                map.put("name",names.get(i));
                maps1.add(map);
            }

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data",maps1);
            jsonObject.put("legend",legend);

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
//            xdata.remove(0);
            xAxis.put("data",xdata);
            jsonObject.put("xAxis",xAxis);


            jsonObject.put("series",series);
            jsonObject.put("yAxis",yAxis);



            ArrayList<ChartSmartVo> newLista = new ArrayList<>();
            ChartSmartVo chartSmartVo = new ChartSmartVo();
            newLista.add(chartSmartVo);
            System.out.println(JSON.toJSONString(jsonObject));
            return AjaxResult.success(newLista);*/
        }

        // 获取dataset节点并替换source节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        for (JsonNode node : dataset) {
            if (node.has("source")) {
                ((ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                break;
            }
        }

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }


        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        String updatedChartValue = chartValueJson.toString();
        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);

    }

    /*    @PostMapping("/doubley/importData")
        public AjaxResult doubley(MultipartFile file,Long configId) throws Exception {
            XSSFWorkbook workbook = null;
            try {
                workbook = new XSSFWorkbook(file.getInputStream());
                Sheet sheet = workbook.getSheetAt(1);
                //获取
                List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

                if (CollectionUtils.isEmpty(getdata)) {
                    return AjaxResult.error("请填充数据");
                }

                List<String> xdata = new ArrayList<>();
                List<String> names = new ArrayList<>();
                List<List<String>> strings = new ArrayList<>();
                for (int i = 0; i < getdata.size(); i++) {
                    List<String> list = getdata.get(i);
                    List<String> datas =new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            if (i1==0) {
                                names.add(s);
                            }else {
                                datas.add(s);
                            }
                        }
                    }
                    if (i==0) {
                        xdata.addAll(datas);
                    }else {
                        strings.add(datas);
                    }
                }


                ChartConfigs configs = chartConfigsService.getById(configId);
                JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

                List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
                List<JSONObject> yAxis = (List<JSONObject>) jsonObject.get("yAxis");
                for (int i = 0; i < series.size(); i++) {
                    if (i==0) {
                        JSONObject jsonObject1 = series.get(i);
                        jsonObject1.put("name",names.get(i+1));
                        List<String> list = strings.get(i);
                        //求出最大值（绝对值）并返回把最大值最高位+1的数值
                        int max = ExcelDataUtils.Max(list);
                        int min = ExcelDataUtils.Min(list);
                        JSONObject jsonObject2 = yAxis.get(i);
                        jsonObject2.put("max",max);
                        jsonObject2.put("min",min);
                        jsonObject2.put("name",names.get(i+1));
                        List<Map> maps = new ArrayList<>();
                        for (int i1 = 0; i1 < list.size(); i1++) {
                            double s = Double.parseDouble(list.get(i1));
                            Map map = new HashMap<>();
                            map.put("value",s);
                            if (s>0) {
                                map.put("itemStyle",rightColor);
                            }else {
                                map.put("itemStyle",leftColor);
                            }
                            maps.add(map);
                        }
                        jsonObject1.put("data",maps);
                    }else {
                        JSONObject jsonObject1 = series.get(i);
                        jsonObject1.put("name",names.get(i+1));
                        List<String> list = strings.get(i);
                        int max = ExcelDataUtils.Max(list);
                        int min = ExcelDataUtils.Min(list);
                        JSONObject jsonObject2 = yAxis.get(i);
                        jsonObject2.put("max",max);
                        jsonObject2.put("min",min);
                        jsonObject2.put("name",names.get(i+1));
                        jsonObject1.put("data",list);
                    }
                }

                List<Map> maps1 = new ArrayList<>();
                for (int i = 1; i < names.size(); i++) {
                    Map map = new HashMap<>();
                    map.put("name",names.get(i));
                    maps1.add(map);
                }

                JSONObject legend = (JSONObject) jsonObject.get("legend");
                legend.put("data",maps1);
                jsonObject.put("legend",legend);

                JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
    //            xdata.remove(0);
                xAxis.put("data",xdata);
                jsonObject.put("xAxis",xAxis);


                jsonObject.put("series",series);
                jsonObject.put("yAxis",yAxis);

                return AjaxResult.success(JSON.toJSONString(jsonObject));
            }finally {
                workbook.close();
            }
        }*/
    @PostMapping("/doubley/v2/importData")
    public AjaxResult v2doubley(@RequestBody List<Map<String, Object>> dataList
            , Long configId) throws Exception {
        //  XSSFWorkbook workbook = null;
        try {
  /*          workbook = new XSSFWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheetAt(1);*/

            Sheet sheet = ExcelUtils.convertDataListToSheets(dataList);
            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata = new ArrayList<>();
            List<String> names = new ArrayList<>();
            List<List<String>> strings = new ArrayList<>();
            for (int i = 0; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);
                List<String> datas = new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1 == 0) {
                            names.add(s);
                        } else {
                            datas.add(s);
                        }
                    }
                }
                if (i == 0) {
                    xdata.addAll(datas);
                } else {
                    strings.add(datas);
                }
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());

            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            List<JSONObject> yAxis = (List<JSONObject>) jsonObject.get("yAxis");
            for (int i = 0; i < series.size(); i++) {
                if (i == 0) {
                    JSONObject jsonObject1 = series.get(i);
                    jsonObject1.put("name", names.get(i + 1));
                    List<String> list = strings.get(i);
                    //求出最大值（绝对值）并返回把最大值最高位+1的数值
                    int max = ExcelDataUtils.Max(list);
                    int min = ExcelDataUtils.Min(list);
                    JSONObject jsonObject2 = yAxis.get(i);
                    jsonObject2.put("max", max);
                    jsonObject2.put("min", min);
                    jsonObject2.put("name", names.get(i + 1));
                    List<Map> maps = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        double s = Double.parseDouble(list.get(i1));
                        Map map = new HashMap<>();
                        map.put("value", s);
                        if (s > 0) {
                            map.put("itemStyle", rightColor);
                        } else {
                            map.put("itemStyle", leftColor);
                        }
                        maps.add(map);
                    }
                    jsonObject1.put("data", maps);
                } else {
                    JSONObject jsonObject1 = series.get(i);
                    jsonObject1.put("name", names.get(i + 1));
                    List<String> list = strings.get(i);
                    int max = ExcelDataUtils.Max(list);
                    int min = ExcelDataUtils.Min(list);
                    JSONObject jsonObject2 = yAxis.get(i);
                    jsonObject2.put("max", max);
                    jsonObject2.put("min", min);
                    jsonObject2.put("name", names.get(i + 1));
                    jsonObject1.put("data", list);
                }
            }

            List<Map> maps1 = new ArrayList<>();
            for (int i = 1; i < names.size(); i++) {
                Map map = new HashMap<>();
                map.put("name", names.get(i));
                maps1.add(map);
            }

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            legend.put("data", maps1);
            jsonObject.put("legend", legend);

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
//            xdata.remove(0);
            xAxis.put("data", xdata);
            jsonObject.put("xAxis", xAxis);


            jsonObject.put("series", series);
            jsonObject.put("yAxis", yAxis);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } finally {
//           workbook.close();
        }
    }

    //堆叠折线图
    @PostMapping("/stackedLine/importData")
    public AjaxResult stackedLine(@RequestBody ChartDateTto chartDateTto) throws Exception {

        return null;
    }


    /*    //堆叠折线图
        @PostMapping("/stackedLine/importData")
        public AjaxResult stackedLine(MultipartFile file,Long configId) throws Exception {
            XSSFWorkbook workbook = null;
            try {
                workbook = new XSSFWorkbook(file.getInputStream());
                Sheet sheet = workbook.getSheetAt(1);
                //获取
                List<List<String>> getdata = ExcelDataUtils.getColData(sheet);


                if (CollectionUtils.isEmpty(getdata)) {
                    return AjaxResult.error("请填充数据");
                }

                List<String> xdata =new ArrayList<>();
                List<String> xlist = getdata.get(0);
                for (int i = 1; i < xlist.size(); i++) {
                    String s = xlist.get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        xdata.add(s);
                    }
                }
    //        xdata.remove(0);

                List<ChartXDataVo> chartXDataVos = new ArrayList<>();
                List<String> ydata = new ArrayList<>();
                List<List<Object>>  groupList= new ArrayList<>();
                for (int i = 1; i < getdata.size(); i++) {
                    List<String> list = getdata.get(i);

                    List<Object> cellData = new ArrayList<>();
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        String s = list.get(i1);
                        if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                            if (i1==0) {
                                ydata.add(s);
                            }else {
                                cellData.add(Double.parseDouble(s));
                            }
                        }
                    }
                    groupList.add(cellData);
                }


                ChartConfigs configs = chartConfigsService.getById(configId);
                JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
                jsonObject.put("dataset", chartXDataVos);

                List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
                JSONObject jsonObject1 = series.get(1);

                ArrayList<JSONObject> jsonObjects = new ArrayList<>();
                for (int i = 0; i < ydata.size() ; i++) {
                    JSONObject jsonObject2 = new JSONObject();
                    jsonObject2.putAll(jsonObject1);
                    jsonObject2.put("data", groupList.get(i));
                    jsonObject2.put("name", ydata.get(i));
                    jsonObjects.add(jsonObject2);
                }
                jsonObject.put("series", jsonObjects);

                JSONObject legend = (JSONObject) jsonObject.get("legend");
    //            xdata.remove(0);
                List<ChartLegendData> chartLegendData = new ArrayList<>();
                for (int i = 0; i < ydata.size(); i++) {
                    ChartLegendData data = new ChartLegendData();
                    data.setName(ydata.get(i));
                    chartLegendData.add(data);
                }
                legend.put("data", chartLegendData);
                jsonObject.put("legend", legend);

                JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
                xAxis.put("data",xlist);
                jsonObject.put("xAxis", xAxis);


                return AjaxResult.success(JSON.toJSONString(jsonObject));
            }finally {
                workbook.close();
            }
        }*/


    public static List<List<String>> readDataVertically(List<Map<String, Object>> dataList) {
        List<List<String>> getdata = new ArrayList<>();

        // Extract column headers from the first map entry
        Map<String, Object> firstRow = dataList.get(0);
        List<String> columns = new ArrayList<>();
        firstRow.forEach((key, value) -> columns.add(key));
        getdata.add(columns);

        // Read data vertically
        for (int i = 0; i < columns.size(); i++) {
            List<String> columnData = new ArrayList<>();
            for (int j = 1; j < dataList.size(); j++) {
                Map<String, Object> row = dataList.get(j);
                Object cellValue = row.get(columns.get(i));
                columnData.add(cellValue != null ? cellValue.toString() : "");
            }
            getdata.add(columnData);
        }

        return getdata;
    }

    //前8行是空白
    public static Sheet convertDataListToSheets(List<Map<String, Object>> dataList) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        // Insert 8 blank rows before starting the data
        for (int i = 0; i < 8; i++) {
            sheet.createRow(i);
        }

        // Populate sheet with data starting from the ninth row
        int rowIndex = 8; // Start from the ninth row
        for (Map<String, Object> data : dataList) {
            Row row = sheet.createRow(rowIndex++);
            int cellIndex = 0;
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                Cell cell = row.createCell(cellIndex++);
                setCellValue(cell, entry.getValue());
            }
        }

        return sheet;
    }

    // Helper method to set cell value based on data type
    public static void setCellValue(Cell cell, Object value) {
        if (value == null) {
            cell.setCellValue("");
        } else if (value instanceof String) {
            cell.setCellValue((String) value);
        } else if (value instanceof Double) {
            cell.setCellValue((Double) value);
        } else if (value instanceof Integer) {
            cell.setCellValue((Integer) value);
        } else {
            cell.setCellValue(value.toString());
        }
    }

    @PostMapping("/stackedLine/v2/importData")
    public AjaxResult vstackedLine(@RequestBody List<Map<String, Object>> dataList, Long configId) {
        XSSFWorkbook workbook = null;
        try {
            Sheet sheet = convertDataListToSheets(dataList);

            // 获取列数据
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata = new ArrayList<>();
            if (!getdata.isEmpty()) {
                List<String> xlist = getdata.get(0);
                for (int i = 1; i < xlist.size(); i++) {
                    String s = xlist.get(i);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        xdata.add(s);
                    }
                }
            }

            List<String> ydata = new ArrayList<>();
            List<List<Object>> groupList = new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);

                List<Object> cellData = new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1 == 0) {
                            ydata.add(s);
                        } else {
                            try {
                                cellData.add(Double.parseDouble(s));
                            } catch (NumberFormatException e) {
                                // Handle parsing error if needed
                                e.printStackTrace(); // 输出异常信息到日志
                            }
                        }
                    }
                }
                groupList.add(cellData);
            }

            ChartConfigs configs = chartConfigsService.getById(configId);
            if (configs == null) {
                return AjaxResult.error("未找到对应的配置");
            }

            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            if (jsonObject == null) {
                return AjaxResult.error("配置数据为空");
            }

            jsonObject.put("dataset", new ArrayList<>()); // chartXDataVos was never populated in your code

            // 处理 series 数据
            Object seriesObj = jsonObject.get("series");
            if (!(seriesObj instanceof JSONObject)) {
                return AjaxResult.error("系列数据格式错误");
            }
            JSONObject series = (JSONObject) seriesObj;
            JSONObject seriesItem = series.getJSONObject("series1"); // 替换为您实际的 series 字段名

            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < ydata.size(); i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(seriesItem);
                jsonObject2.put("data", groupList.get(i));
                jsonObject2.put("name", ydata.get(i));
                jsonObjects.add(jsonObject2);
            }
            jsonObject.put("series", jsonObjects);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
            if (legend == null) {
                return AjaxResult.error("图例数据为空");
            }
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 0; i < ydata.size(); i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(ydata.get(i));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            if (xAxis == null) {
                return AjaxResult.error("X轴数据为空");
            }
            xAxis.put("data", xdata);
            jsonObject.put("xAxis", xAxis);

            return AjaxResult.success(JSON.toJSONString(jsonObject));
        } catch (Exception e) {
            e.printStackTrace(); // 输出异常信息到日志
            return AjaxResult.error("处理数据时发生异常: " + e.getMessage());
        } finally {
            try {
                if (workbook != null) {
                    workbook.close();
                }
            } catch (IOException e) {
                e.printStackTrace(); // 输出异常信息到日志
            }
        }
    }



 /*   // 堆叠折线图
    @PostMapping("/stackedLine/v2/importData")
    public AjaxResult vstackedLine(@RequestBody List<Map<String, Object>> dataList,Long configId) throws Exception {
        XSSFWorkbook workbook = null;
        try {

            Sheet sheet= convertDataListToSheets(dataList);


            //获取
            List<List<String>> getdata = ExcelDataUtils.getColData(sheet);

            System.out.println(getdata);
            if (CollectionUtils.isEmpty(dataList)) {
                return AjaxResult.error("请填充数据dataList");
            }
          //  List<List<String>> getdata = wrapListMapToList(dataList);

            if (CollectionUtils.isEmpty(getdata)) {
                return AjaxResult.error("请填充数据");
            }

            List<String> xdata =new ArrayList<>();
            List<String> xlist = getdata.get(0);
            for (int i = 1; i < xlist.size(); i++) {
                String s = xlist.get(i);
                if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                    xdata.add(s);
                }
            }
//        xdata.remove(0);

            List<ChartXDataVo> chartXDataVos = new ArrayList<>();
            List<String> ydata = new ArrayList<>();
            List<List<Object>>  groupList= new ArrayList<>();
            for (int i = 1; i < getdata.size(); i++) {
                List<String> list = getdata.get(i);

                List<Object> cellData = new ArrayList<>();
                for (int i1 = 0; i1 < list.size(); i1++) {
                    String s = list.get(i1);
                    if (!ObjectUtils.isEmpty(s) && !s.equals("")) {
                        if (i1==0) {
                            ydata.add(s);
                        }else {
                            cellData.add(Double.parseDouble(s));
                        }
                    }
                }
                groupList.add(cellData);
            }


            ChartConfigs configs = chartConfigsService.getById(configId);
            JSONObject jsonObject = JSON.parseObject(configs.getConfigValue());
            jsonObject.put("dataset", chartXDataVos);

            // 检查 'series' 是否为 JSON 数组
            Object seriesObj = jsonObject.get("series");
            if (!(seriesObj instanceof JSONArray)) {
                return AjaxResult.error("JSON 结构中 'series' 不符合预期");
            }

            // 将 'series' 转换为 JSONArray
            JSONArray seriesArray = (JSONArray) seriesObj;
            // 如果数组长度不足 2，返回错误响应
            if (seriesArray.size() < 2) {
                return AjaxResult.error("JSON 中 'series' 元素不足");
            }


            List<JSONObject> series = (List<JSONObject>) jsonObject.get("series");
            JSONObject jsonObject1 = series.get(1);



            System.out.println(jsonObject1);


            ArrayList<JSONObject> jsonObjects = new ArrayList<>();
            for (int i = 0; i < ydata.size() ; i++) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.putAll(jsonObject1);
                jsonObject2.put("data", groupList.get(i));
                jsonObject2.put("name", ydata.get(i));
                jsonObjects.add(jsonObject2);
            }
            jsonObject.put("series", jsonObjects);

            JSONObject legend = (JSONObject) jsonObject.get("legend");
//            xdata.remove(0);
            List<ChartLegendData> chartLegendData = new ArrayList<>();
            for (int i = 0; i < ydata.size(); i++) {
                ChartLegendData data = new ChartLegendData();
                data.setName(ydata.get(i));
                chartLegendData.add(data);
            }
            legend.put("data", chartLegendData);
            jsonObject.put("legend", legend);

            JSONObject xAxis = (JSONObject) jsonObject.get("xAxis");
            xAxis.put("data",xlist);
            jsonObject.put("xAxis", xAxis);


            return AjaxResult.success(JSON.toJSONString(jsonObject));
        }finally {
          //  workbook.close();
        }
    }*/

    //图表信息的颜色配置
    private static Map rightColor = new HashMap();

    static {
        rightColor.put("type", "linear");
        rightColor.put("x", 0);
        rightColor.put("y", 0);
        rightColor.put("x2", 0);
        rightColor.put("y2", 1);
        List<Map> maps = new ArrayList<>();
        Map map1 = new HashMap<>();
        map1.put("offset", 0);
        map1.put("color", "#ff9569");
        maps.add(map1);
        Map map2 = new HashMap<>();
        map2.put("offset", 1);
        map2.put("color", "#e92758");
        maps.add(map2);
        rightColor.put("colorStops", maps);
        rightColor.put("global", false);

        List<Integer> list = new ArrayList<>();
        list.add(30);
        list.add(30);
        list.add(0);
        list.add(0);
        rightColor.put("barBorderRadius", list);
    }

    private static Map leftColor = new HashMap();

    static {
        leftColor.put("type", "linear");
        leftColor.put("x", 0);
        leftColor.put("y", 0);
        leftColor.put("x2", 0);
        leftColor.put("y2", 1);
        List<Map> maps = new ArrayList<>();
        Map map1 = new HashMap<>();
        map1.put("offset", 0);
        map1.put("color", "#e92758");
        maps.add(map1);
        Map map2 = new HashMap<>();
        map2.put("offset", 1);
        map2.put("color", "#ff9569");
        maps.add(map2);
        leftColor.put("colorStops", maps);
        leftColor.put("global", false);

        List<Integer> list = new ArrayList<>();
        list.add(0);
        list.add(0);
        list.add(30);
        list.add(30);
        leftColor.put("barBorderRadius", list);
    }

}
